package cn.wizzer.app.crown.modules.services.impl.result;


import cn.wizzer.app.crown.modules.models.CrownConfig;
import cn.wizzer.app.crown.modules.models.CrownLeague;
import cn.wizzer.app.crown.modules.models.CrownLeagueResult;
import cn.wizzer.app.crown.modules.models.constant.CrownConstant;
import cn.wizzer.app.crown.modules.models.constant.CrownPattern;
import cn.wizzer.app.crown.modules.services.CrownConfigService;
import cn.wizzer.app.crown.modules.services.CrownLeagueService;
import cn.wizzer.app.crown.modules.services.CrownMatchService;
import cn.wizzer.app.crown.modules.services.result.LeagueResultService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Dao;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@IocBean(args = {"refer:dao"})
@Service(interfaceClass = LeagueResultService.class)
public class LeagueResultServiceImpl extends BaseServiceImpl<CrownLeagueResult> implements LeagueResultService {
    public LeagueResultServiceImpl(Dao dao) {
        super(dao);
    }

    private static final Log log = Logs.get();

    @Inject
    private RedisService redisService;

    @Inject
    private CrownMatchService matchService;

    @Inject
    private CrownLeagueService leagueService;
    @Inject
    @Reference(retries = 0, timeout = 500000)
    private CrownConfigService crownConfigService;

    private static final Pattern LEAGUE_COMPILE = Pattern.compile("<span class=\"acc_cont_bold\">([\\s\\S]*?)</span>");

    @Override
    public void crawler(CrownLeague league, CrownConfig crownConfig) {
        //66.133.86.54/app/member/account/result/FS_result_new.php?uid=1bn09d57m22923423l281463&gtype=FS&game_id=159590&langx=zh-cn
        String tailUrl = "/app/member/account/result/FS_result_new.php?uid=" + crownConfig.getUid()
                + "&gtype=FS&game_id=" + league.getChampionId() + "&langx=" + CrownConstant.LANGX;
        String body = crownConfigService.getCrownData(tailUrl, crownConfig, CrownConstant.RE_TRY_TIMES);
// 检测账号被登出
        if (crownConfigService.checkLogout(body, crownConfig)) {
            // 重试登录(包括重试三次、自动请求新地址)
//            crownConfigService.loginHg(crownConfig);
//            // 如果登录成功 重新请求数据
//            if (!crownConfig.isLogout()) {
//                this.crawler(league, crownConfig);
//                return;
//            }
            return;
        }
        // body是空，监测到皇冠维护都return
        if (Strings.isBlank(body)) {
            log.error("冠军爬取赛果失败，body为空");
            return;
        } else if (crownConfigService.checkMaintain(body, crownConfig)) {
            return;
        }


        // 解析数据
        Matcher m = LEAGUE_COMPILE.matcher(body);
        String championName = "";
        ArrayList<CrownLeagueResult> subResults = new ArrayList<>();
        log.info("开始解析冠军赛事赛果");
        // 冠军赛果有可能不只一条，循环匹配
        while (m.find()) {
            championName = CrownPattern.HTML_PATTERN.matcher(m.group()).replaceAll("").trim();
//                championName = m.group()
//                        .replaceAll("<span class=\"acc_cont_bold\">", "")
//                        .replaceAll("</span>", "").trim();
            // 联赛赛果表插入记录
            CrownLeagueResult result = new CrownLeagueResult();
            result.setLeagueId(league.getId());
            result.setWinner(championName);
            subResults.add(result);
        }
        league.setSubResults(subResults);
        // 更新赛事结束状态
        if ("取消".equals(championName)) {
            league.setStage(CrownConstant.OTHER);
        } else {
            league.setStage(CrownConstant.FINISHED);
        }
        // 调用更新赛果方法
        leagueService.updateResult(league);
        log.info("冠军赛果爬取完毕：" + league);
    }
}
